package com.objectdraw.client.objectdraw;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * 
 * 
 * @author jgulik, cfruehan
 *
 */
public class ControlPanelListener implements ActionListener, ItemListener {

	protected DrawingCanvas canvas;

	ControlPanelListener(DrawingCanvas canvas)  {
		this.canvas=canvas;
	}

	/**
	 * listens to buttons on control panel and sends the appropriate message based
	 * upon appropriate click.
	 */
	public void actionPerformed(ActionEvent e) {
		if(((JButton)e.getSource()).getText()=="Clear" && canvas.control){
			canvas.clearCanvas();
			canvas.com.sendMessage("clear ");
		}else if(((JButton)e.getSource()).getText()=="Request Control"){
			canvas.com.sendMessage("Control request");
		}else if(((JButton)e.getSource()).getText()=="Release Control"){
			canvas.com.sendMessage("Control release");
			canvas.control=false;
		}else if(((JButton)e.getSource()).getText()=="Update Canvas"){
			canvas.com.sendMessage("FullUpdate ");
		}
	}

	/**
	 * listens to color selection combo box.  Changes canvas selected color upon change
	 * and sends message to server to inform it of a change in color selection.
	 */
	public void itemStateChanged(ItemEvent e)  {
		if (e.getStateChange() == ItemEvent.SELECTED && canvas.control) {
			canvas.setPenColor(itemToColor(e.getItem()));
			canvas.com.sendMessage("color " + e.getItem().toString());
			if (canvas.magicBox.active){
				try{
					((SelectTool)canvas.getCurrentTool()).selected.setmycolor(canvas.penColor);
					canvas.repaint();
				}catch(Exception x){
				}
			}
		}
	}

	protected Color itemToColor(Object item) {

		if("black".equals(item)) {
			return Color.black;
		}
		else if("blue".equals(item)) {
			return Color.blue;
		}
		else if("green".equals(item)) {
			return Color.green;
		}
		else {
			return Color.red;
		}

	}

} 
